﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Linq;
using Xom.Attributes;
using Xom.Elements;
using Xom.Engine;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xom.UnitTests.Mocks;

namespace Xom.UnitTests
{
    /// <summary>
    ///This is a test class for XmlObjectMapperTest and is intended
    ///to contain all XmlObjectMapperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class XmlObjectMapperTest
    {

        public void TestCtor()
        {
            IXmlObjectMapper mapper = XmlObjectMapper.Create();
            SimpleXmlTestObject testObject2 = mapper.Load("", typeof(SimpleXmlTestObject)) as SimpleXmlTestObject;

            SimpleXmlTestObject testObject = XmlObjectMapper.Load("", typeof (SimpleXmlTestObject)) as SimpleXmlTestObject;
        }


        /// <summary>
        ///A test for XmlObjectMapper Constructor
        ///</summary>
        [TestMethod()]
        public void ReadSimpleXmlTest()
        {
            //arrange
            XmlDocument document = GetSimpleXmlTestFile();
            XmlDocumentEngine mapper = new XmlDocumentEngine();

            //act
            SimpleXmlTestObject testObject = mapper.StartMapping(document, typeof (SimpleXmlTestObject)) as SimpleXmlTestObject;

            //assert
            Assert.IsNotNull(testObject);
            uint id = 100;
            Assert.AreEqual(id, testObject.Id);
            Assert.AreEqual(10.00, testObject.Value);
            Assert.IsNull(testObject.IsActive);
            Assert.IsNull(testObject.Control);
            Assert.AreEqual(true, string.IsNullOrEmpty(testObject.Name));
        }

        [TestMethod()]
        public void WriteMissingSimpleXmlTest()
        {
            //arrange
            XmlDocument originalDocument = GetSimpleXmlTestFile();
            XmlDocument document = GetSimpleXmlTestFile();
            XmlDocumentEngine mapper = new XmlDocumentEngine();
            SimpleXmlTestObject testObject = mapper.StartMapping(document.LastChild, typeof(SimpleXmlTestObject)) as SimpleXmlTestObject;

            //act
            testObject.Name = "test";
            testObject.IsActive = false;
            testObject.Control = new SimpleSubXmlTestObject();
            testObject.Control.Info = "XYZ";

            //assert
            Assert.AreNotEqual(originalDocument.OuterXml, document.OuterXml);
            Assert.IsTrue(document.OuterXml.Contains(@"<sub>XYZ"));
            Assert.IsTrue(document.OuterXml.Contains(@"<Active>false"));
            Assert.IsTrue(document.OuterXml.Contains(@"<simple name=""test"" Id=""100"""));
        }

        [TestMethod()]
        public void ParseXmlWithLinqToXMl()
        {
            IXmlObjectMapper mapper = XmlObjectMapper.Create();
            object testObject = mapper.Parse(GetComplexXml(), typeof(SimpleXmlTestObject));
        }

        private XmlDocument GetSimpleXmlTestFile()
        {
            XmlDocument document = new XmlDocument();
            document.LoadXml(GetSimpleXml());

            return document;
        }

        private string GetSimpleXml()
        {
            return @"<?xml version=""1.0""?>   
                                <simple Id=""100""> 
                                    <Value>10.00</Value>
                                    <Active/>   
                                </simple>";
        }

        private string GetComplexXml()
        {
            return @"<?xml version=""1.0""?>   
                                <simple Id=""100""> 
                                    <Value>10.00</Value>
                                    <Active/>
                                    <sub index=""0"">abc</sub>   
                                </simple>";
        }

        private string GetListSimpleXml()
        {
            return @"<?xml version=""1.0""?>
                            <container>   
                                <simple Id=""100""> 
                                    <Value>10.00</Value>
                                    <Active/>
                                    <sub index=""0"">abc</sub>   
                                </simple>
                                <simple Id=""200""> 
                                    <Value>9</Value>
                                    <Active/>
                                    <sub index=""0"">def</sub>   
                                </simple>
                            </container>";
        }
    }

    
}
